package com.spider.gk.common;

import java.util.HashMap;
import java.util.Map;

public class ContentAbstract {

	private String content;
	
	public ContentAbstract(String content){
		this.content = content;
	}
	
	public Map<String,String> process(){
		
		boolean isWant=false;
		
		if(this.content!=null && this.content.length()>0){
			
			Map<String,String> result = new HashMap<String,String>();
			
			int titleIndex = content.indexOf("class=\"context\">");
			int titleIndexEnd = content.indexOf("</div>",titleIndex+1);
			
			//System.out.println(titleIndex);
			//System.out.println(titleIndexEnd);
			
			if(titleIndex>0 && titleIndexEnd>0){
				result.put("title",content.substring(titleIndex+16, titleIndexEnd));
			}
			
			int priceIndex = content.indexOf("present-price");
			int priceIndexEnd = content.indexOf("</div>", priceIndex+1);
			if(priceIndex>0 && priceIndexEnd>0){
				result.put("price", content.substring(priceIndex+16, priceIndexEnd));
			}
			
			int itemIndex = content.indexOf("http://item.taobao.com/item.htm");
			int itemIndexEnd = content.indexOf("\"",itemIndex+1);
			//System.out.println(itemIndex);
			//System.out.println(itemIndexEnd);
			if(itemIndex>0 && itemIndexEnd >0){
				result.put("itemUrl", content.substring(itemIndex, itemIndexEnd));
			}
			else{
				int nitemIndex = content.indexOf("http://s.click.taobao.com");
				int nitemIndexEnd = content.indexOf("\"",nitemIndex+1);
				//System.out.println(nitemIndex);
				//System.out.println(nitemIndexEnd);
				if(nitemIndex>0 && nitemIndexEnd >0){
					result.put("itemUrl", content.substring(nitemIndex, nitemIndexEnd));
				}
			}
			
			int picIndex = content.indexOf("http://img");
			int picIndexEnd = content.indexOf("\"",picIndex+1);
			if(picIndex>0 && picIndexEnd>0){
				//System.out.println(picIndex);
				//System.out.println(picIndexEnd);
				result.put("itemPic", content.substring(picIndex, picIndexEnd));
			}
			if(result!=null && result.containsKey("price")){
				String priceStr = result.get("price");
				System.out.println(priceStr);
				double priceDouble = Double.parseDouble(priceStr);
				if(priceDouble>50 && priceDouble<=105){
					isWant = true;
				}
			}
			if(!isWant){
				return new HashMap<String ,String>();
			}
			return result;
		}
		
		return new HashMap<String ,String>();
	}
	
}
